import pymysql

from common.log_tools import log


#1.数据库连接
def connect():
    try:
        conn =    pymysql.connect(user="root",
                    password="",
                    host="192.168.1.165",
                    database="ranzhi",
                    port=3306,
                    charset="utf8"
                    )
    except Exception as e:
        log.error(f"数据库打开错误{e}")
    else:
        return conn
#2.数据库操作
def excute(conn,sql):
    #获取游标
    try:
        r = conn.cursor()
        result=r.execute(sql)
        conn.commit()
        return  result
    except Exception as e:
        print(e)
        log.error(f"数据库执行错误{e}")
#3.数据库断开连接
def disconnect(conn):
    conn.close()
if __name__ == '__main__':
    conn = connect()
    # excute(conn,"delete from sys_user where account like 'test% ")
    disconnect(conn)